angular.module("crawlerApp").controller("dataFormatCtrl", function ($scope, $http, $state, $stateParams, menuService, $rootScope) {

    $scope.config = $stateParams.config;
    indexIsExist($scope.config.dataType);
    $scope.commit = function (dataFormat) {
        dataFormat.dataType=angular.copy($scope.config.dataType);
        $http.post("crawler/dataFormat/addFormat" , dataFormat).then(function (response) {
            getDataFormat($scope.config.dataType);
        });
    };
    getDataFormat($scope.config.dataType);
    
    function getDataFormat(type){
        $http.get("crawler/dataFormat/getFormat/"+type).then(function (response) {
                $scope.dataFormats=response.data;
        })
    }

    $scope.goBack=function () {
        $state.go("dataType", {config: null});
    };

    $scope.confirmCreate=function () {
        getDataFormat($scope.config.dataType);
        $http.post("crawler/dataType/confirmCreate", $scope.dataFormats).then(function (response) {
                if(response.status=='200'){
                    alert("创建成功！");
                }
            getDataFormat($scope.config.dataType);
        });
    };


    $scope.delete=function(data){
      $scope.dataToDelete=angular.copy(data);
    };
    $scope.confirmDelete=function(){
      $http.delete("crawler/dataFormat/delete/"+ $scope.dataToDelete.id).then(function (response) {
          if(response.status=='200'){
              alert("删除成功！");
          }
      });
    };
    function indexIsExist(data){
        $http.post("crawler/index/indexIsExist",data).then(function (response) {
                $scope.isExist=response.data;
        })
    }
});